<?php
/**
 * Created by PhpStorm.
 * User: freethy
 * Date: 2018-05-11
 * Time: 11:02
 */


//全局锁 sync同步true，异步false
function get_lock($name,$sync = false){
    $root = ROOT_PATH . "runtime\\lock\\$name";
    $fp = fopen($root, "w");
    if($sync){
        $p = LOCK_EX;
    } else {
        $p = LOCK_EX | LOCK_NB;
    }
    
    if (flock($fp, $p)) {
        ftruncate($fp, 0);
        fwrite($fp, "lock.".time());
        fflush($fp);
        return $fp;
    } else {
        return null;
    }
}

//释放锁
function unlock($fp){
    flock($fp, LOCK_UN);
}
